/**
 * Created by shantong on 2018/6/9.
 */
import Http from '../utils/Http'
import Cache from '../utils/Cache'
import store from '../store'
import * as Actions from '../store/reducers/Actions'
import config from '../config/config'

const RoleActiveStatus = 0;
const RoleDisabledStatus = 1;

export const login = async (params) => {
	try {
		let result = await Http.post('login', params);
		console.log(result);
		Cache.set('token', result.result.token);
		Cache.set('isLogin', true);
		return result;
	} catch (err) {
		console.log(err);
	}
}

export const getUserInfo = async () => {
	try {
		let result = await Http.get('get-user-info');
		console.log(result, 'userInfo');
		Cache.set('userInfo', result.result);
		store.dispatch({
			'type': Actions.setUserInfo,
			'userInfo': result.result,
		})
		return result;
	} catch (err) {
		console.log(err);
	}
}

export const getTodoList = async () => {
	try {
		let result = await Http.get('get-todo-list');
		store.dispatch({
			'type': Actions.setTodos,
			'todolist': result.result,
		})
		return result;
	} catch (err) {
		console.log(err);
	}
}

export const addTodo = async name => {
	try {
		let result = await Http.post('add-todo', {name: name});
		console.log(333333,result);
		store.dispatch({
			'type': Actions.addTodo,
			'id': result.result.id,
			name
		})
		return result;
	} catch (err) {
		console.log(err);
	}
}

export const changeTodo = async id => {
	try {
		let result = await Http.post('change-todo', {id: id});
		store.dispatch({
			'type': Actions.toggleTodo,
			id
		})
		return result;
	} catch (err) {
		console.log(err);
	}
}

export const getMenu = async () => {
	try {
		let result = await Http.get('setting/get-menus');
		store.dispatch({
			'type': Actions.setMenu,
			'result': result.result
		})
		return result;
	} catch (err) {
		console.log(err);
	}
}

export const saveMenu = async (type, data) => {
	try {
		let url = `setting/${type}-menus`;
		let result = await Http.post(url, data);
		console.log(result);
		store.dispatch({
			'type': Actions.setMenu,
			'result': result.result
		})
		return result;
	} catch (err) {
		console.log(err);
	}
}
export const deleteMenu = async id => {
	try {
		let result = await Http.post('setting/delete-menus', {id});
		console.log(result);
		store.dispatch({
			'type': Actions.setMenu,
			'result': result.result
		})
		return result;
	} catch (err) {
		console.log(err);
	}
}
export const getPermission = async () => {
	try {
		let result = await Http.get('permissions');
		store.dispatch({
			'type': Actions.setPermission,
			'permission': result.result
		})
		return result;
	} catch (err) {
		console.log(err);
	}
}
export const changeStatus = async (id, permission, key, checked) => {
	try {
		let result = await Http.post('permission/change-status', { id });
		permission.data[ key ].status = checked ? 0 : 1;
		store.dispatch({
			'type': Actions.setPermission,
			permission: {...permission},
		})
		return result;
	} catch (err) {
		console.log(err);
	}
}
export const savePermission = async (type, data) => {
	try {
		let url = `permission/${type}-permission`;
		let result = await Http.post(url, data);
		store.dispatch({
			'type': Actions.setPermission,
			permission: result.result
		})
		return result;
	} catch (err) {
		console.log(err);
	}
}
export const deletePermission = async id => {
	try {
		let result = await Http.post('permission/delete-permission', { id });
		store.dispatch({
			'type': Actions.setPermission,
			permission: result.result
		})
		return result;
	} catch (err) {
		console.log(err);
	}
}
export const getUsers = async (page=1, pageSize=10) => {
	try {
		let result = await Http.get('users', {page, pageSize});
		store.dispatch({
			'type': Actions.setUsers,
			'users': result.result
		})
		return result.result.count;
	} catch (err) {
		console.log(err);
	}
}
export const changeUserStatus = async (user, index, checked) => {
	try {
		let result = await Http.post('user/change-status', { id: user.id });
		user.status = checked ? config.UserActiveStatus : config.UserDisabledStatus,
		store.dispatch({
			'type': Actions.updateUsers,
			user,
			index,
		})
		return result.code;
	} catch (err) {
		console.log(err);
	}
}

export const saveUsers = async (type, data, index) => {
	try {
		let url = `user/${type}-user`;
		let result = await Http.post(url, data);
		store.dispatch({
			'type': type === 'add' ? Actions.addUsers : Actions.updateUsers,
			user: result.result,
			index: index,
		})
		return result;
	} catch (err) {
		console.log(err);
	}
}
export const deleteUsers = async (id, index) => {
	try {
		let result = await Http.post('user/delete-user', { id });
		store.dispatch({
			'type': Actions.deleteUsers,
			index,
		})
		return result.code;
	} catch (err) {
		console.log(err);
	}
}
export const getRoleBriefList = async () => {
	try {
		let result = await Http.get('permission/role-brief-list');
		console.log(result);
		return result.result;
	} catch (err) {
		console.log(err);
	}
}


export const getRoles = async () => {
	try {
		let result = await Http.get('permission/roles',);
		console.log(result);
		store.dispatch({
			'type': Actions.setRoles,
			'roles': result.result
		})
		return true;
	} catch (err) {
		console.log(err);
	}
}

export const changeRoleStatus = async (role, index, checked) => {
	try {
		let result = await Http.post('permission/change-role-status', { id: role.id });
		role.status = checked ? RoleActiveStatus : RoleDisabledStatus,
			store.dispatch({
				'type': Actions.updateRoles,
				'role': role,
				index,
			})
		return result.code;
	} catch (err) {
		console.log(err);
	}
}

export const deleteRoles = async (id, index) => {
	try {
		let result = await Http.post('permission/delete-roles', { id });
		store.dispatch({
			'type': Actions.deleteRoles,
			index,
		})
		return result.code;
	} catch (err) {
		console.log(err);
	}
}


export const saveRoles = async (type, data, index) => {
	try {
		let url = `permission/${type}-roles`;
		let result = await Http.post(url, data);
		store.dispatch({
			'type': type === 'add' ? Actions.addRoles : Actions.updateRoles,
			role: result.result,
			index: index,
		})
		return result.code;
	} catch (err) {
		console.log(err);
	}
}
/**
 * 获取权限树，用于设置权限
 * @return {[type]} [description]
 */
export const getPermissionTree = async () => {
	try {
		let result = await Http.get('permission/get-permission-tree',);
		console.log(result);
		return result;
	} catch (err) {
		console.log(err);
	}
}

export const getRolePermission = async role_id => {
	try {
		let result = await Http.get('permission/get-role-permissions', { role_id });
		console.log(result);
		return result;
	} catch (err) {
		console.log(err);
	}
}

export const setRolePermission = async (role_id, permissions) => {
	try {
		console.log(role_id, permissions, 99999);
		let result = await Http.post('permission/set-role-permissions', { role_id, permissions });
		console.log(result);
		return result;
	} catch (err) {
		console.log(err);
	}
}
